#!/usr/bin/python2.5
#
# (c) 2010 - Chip Turner
#
# Apache License, blah blah, it's a few hundred lines of python.

import logging
import os
import time
import sys
import pprint
import optparse
import textwrap

import armory_lib
import auction_lib
import auction_storage

def main(args):
  parser = optparse.OptionParser(__doc__)
  parser.add_option("-r", "--realm", help="realm player or guild is on")
  parser.add_option("-c", "--character", help="character to query")
  parser.add_option("-f", "--faction", help="daction to query; 'a' 'h' or 'n'")
  parser.add_option("-a", "--account", help="battle.net account")
  parser.add_option("-d", "--db", help="local mysql db to connect to", default="auction_house")
  parser.add_option("-v", "--verbose", help="show debugging output", action="store_true")
  parser.add_option("", "--eu", help="Use EU Armory instead of US armory",
                    action="store_true")
  options, args = parser.parse_args(args)

  if options.verbose:
    armory_lib.InitLogging(logging.DEBUG)
  else:
    armory_lib.InitLogging()

  if options.eu:
    auction_interface = auction_lib.AuctionHouseInterface("eu", options.account)
  else:
    auction_interface = auction_lib.AuctionHouseInterface("us", options.account)

  if options.realm and options.character:
    print auction_interface.ChangeCharacter(options.realm, options.character)
  if options.faction:
    print auction_interface.ChangeHouse(options.faction)

  storage = auction_storage.AuctionStorage(options.db)
  all_glyphs = auction_interface.FindAllGlyphs()
  for glyph in all_glyphs:
    pprint.pprint(glyph)
    item = auction_storage.AuctionItem(glyph)
    storage.SaveAuction(item)
    storage.connection.commit()


if __name__ == '__main__':
  main(sys.argv[1:])
